package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.RoleDto;
import com.itheima.admin.mapper.RoleMapper;
import com.itheima.admin.pojo.Role;
import com.itheima.admin.service.IRoleService;
import com.itheima.admin.service.IUserService;
import com.itheima.admin.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    public PageVo<RoleVo> queryByPage(PageDto pageDto){
        IPage<Role> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",pageDto.getUsername());
       IPage<Role> roleIPage=roleMapper.selectPage(page, queryWrapper);
        List<RoleVo> collect = roleIPage.getRecords().stream().map(
                role -> {
                    RoleVo roleVo = new RoleVo();
                    BeanUtils.copyProperties(role, roleVo);
                    Date createTime = role.getCreateTime();
                    roleVo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(createTime));
                    return roleVo;

                }
        ).collect(Collectors.toList());
        return new PageVo<RoleVo>(collect,(int)roleIPage.getTotal());
    }

    @Override
    public List<RoleVo> queryAll() {
        List<Role> roles = roleMapper.selectList(null);
        if (roles==null){
            return new ArrayList<>();
        }
        List<RoleVo> collect = roles.stream().map(
                role -> {
                    RoleVo roleVo = new RoleVo();
                    BeanUtils.copyProperties(role, roleVo);
                    Date createTime = role.getCreateTime();
                    roleVo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(createTime));
                    return roleVo;

                }
        ).collect(Collectors.toList());
        return collect;
    }

    @Override
    public boolean addRole(RoleDto roleDto) {

        int insert = roleMapper.insert(roleDto.toRole());

        if (insert>0){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean deleteRole(String id) {
        roleMapper.deleteRoleUser(id);
        roleMapper.deleteRoleModule(id);
        int i = roleMapper.deleteById(id);
        if (i>0){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public boolean roleAuth(Map map) {
        String  roleId = (String) map.get("roleId");
        List<String> moduleIds = (List<String>)map.get("moduleIds");
        int result=roleMapper.addRoleToModule(roleId,moduleIds);
        if (result>0){
            return true;
        }
        else{
            return false;
        }
    }


}
